﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using WebApiCoreDemo.Common;

namespace WebApiCoreDemo.Api.Filters
{
    public class CustomActionFilterAttribute : Attribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var logInfo = $"{DateTime.Now}开始调用{context.RouteData.Values["action"]}：参数为{JsonConvert.SerializeObject(context.ActionArguments)}";
            NlogUtil.WriteInfo(MethodBase.GetCurrentMethod().DeclaringType.FullName, logInfo, "", "WebApiCoreDemo.Api");
            Console.WriteLine("this is CustomActionFilterAttribute.OnActionExecuting()");
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            ObjectResult objectResult = context.Result as ObjectResult;
            var logInfo = $"{DateTime.Now}调用{context.RouteData.Values["action"]}完毕：结果为{JsonConvert.SerializeObject(objectResult.Value)}";
            NlogUtil.WriteInfo(MethodBase.GetCurrentMethod().DeclaringType.FullName, logInfo, "", "WebApiCoreDemo.Api");
            Console.WriteLine("this is CustomActionFilterAttribute.OnActionExecuted()");
        }
    }
}
